using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.DB.Orm
{
    /// <summary></summary>
    [Serializable]
    public class OrmInstruction:InstructionBase
    {
        #region props
        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get { return _ConnStr; }
            set { _ConnStr = value; }
        }

        private string _TblName;
        /// <summary></summary>
        public string TblName
        {
            get { return _TblName; }
            set { _TblName = value; }
        }

        private string _PkField;
        /// <summary></summary>
        public string PK
        {
            get { return _PkField; }
            set { _PkField = value; }
        }

        private Type _PkDataType;
        /// <summary></summary>
        public Type PkDataType
        {
            get { return _PkDataType; }
            set { _PkDataType = value; }
        }
	

        private bool _IsIdentity;
        /// <summary></summary>
        public bool IsIdentity
        {
            get { return _IsIdentity; }
            set { _IsIdentity = value; }
        }

        private OrmActionType _ActionType;
        /// <summary></summary>
        public OrmActionType ActionType
        {
            get { return _ActionType; }
            set { _ActionType = value; }
        }

        private string _BindableObjAssemblyFileName;
        /// <summary></summary>
        public string BindableObjAssemblyFileName
        {
            get { return _BindableObjAssemblyFileName; }
            set { _BindableObjAssemblyFileName = value; }
        }

        private string _BindableObjTypeFullName;
        /// <summary></summary>
        public string BindableObjTypeFullName
        {
            get { return _BindableObjTypeFullName; }
            set { _BindableObjTypeFullName = value; }
        }

        private object _BindableObj;
        /// <summary></summary>
        public object BindableObject
        {
            get { return _BindableObj; }
            set { _BindableObj = value; }
        }
        #endregion
        /// <summary></summary>
        public OrmInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._TblName = string.Empty;
            this._PkField = string.Empty;
            this._PkDataType = typeof (int);
            this._IsIdentity = false;
            this._ActionType = OrmActionType.Insert;
            this._BindableObjAssemblyFileName = string.Empty;
            this._BindableObjTypeFullName = typeof (string).FullName;
            this._BindableObj = string.Empty;
        }
        /// <summary></summary>
        public OrmInstruction(Guid componentID, Guid jobTicketID, 
            string connStr, string tblName, string pkField,
            Type pkType, bool isIdentity, OrmActionType action,
            string assemblyFileName, string objTypeName, object bindableObj) 
            : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._TblName = tblName;
            this._PkField = pkField;
            this._PkDataType = pkType;
            this._IsIdentity = isIdentity;
            this._ActionType = action;
            this._BindableObjAssemblyFileName = assemblyFileName;
            this._BindableObjTypeFullName = objTypeName;
            this._BindableObj = bindableObj;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnectionString", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TblName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PkField", this.PK);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PkType", this.PkDataType.FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IsIdentity", this.IsIdentity.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Action", ((int) this.ActionType).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"BindableObjectAssemblyName",this.BindableObjAssemblyFileName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BindableObjectTypeName", this.BindableObject.GetType().FullName);

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                OrmInstruction instruction = new OrmInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.ConnStr = xNode.Attributes.GetNamedItem("ConnectionString").Value;
                instruction.TblName = xNode.Attributes.GetNamedItem("TableName").Value;
                instruction.PK = xNode.Attributes.GetNamedItem("PkField").Value;
                instruction.PkDataType = Type.GetType(xNode.Attributes.GetNamedItem("PkType").Value);
                instruction.IsIdentity = bool.Parse(xNode.Attributes.GetNamedItem("IsIdentity").Value);
                instruction.ActionType = (OrmActionType) int.Parse(xNode.Attributes.GetNamedItem("Action").Value);
                instruction.BindableObjAssemblyFileName =
                    xNode.Attributes.GetNamedItem("BindableObjectAssemblyName").Value;
                string binFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string assemblyFilePath = Path.Combine(binFolderPath, instruction.BindableObjAssemblyFileName);
                Assembly assem = Assembly.LoadFile(assemblyFilePath);
                instruction.BindableObjTypeFullName = xNode.Attributes.GetNamedItem("BindableObjectTypeName").Value;
                Type bindableObjType = assem.GetType(instruction.BindableObjTypeFullName);
                instruction.BindableObject = Activator.CreateInstance(bindableObjType);
                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }
    }
}
